using admin.application;
using admin.infrastructure;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using Serilog;

var builder = WebApplication.CreateBuilder(args);

// 配置Serilog
Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration)
    .WriteTo.Console() // 确保控制台输出
    .CreateLogger();

builder.Host.UseSerilog();

// 添加CORS配置
builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowAll", policy =>
    {
        policy.AllowAnyOrigin()
              .AllowAnyMethod()
              .AllowAnyHeader()
              .WithExposedHeaders("Content-Type", "Authorization");
    });
});

builder.Services.AddControllers();

// 添加JSON序列化配置
builder.Services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = null; // 保持属性名大小写
    options.JsonSerializerOptions.WriteIndented = true; // 格式化JSON输出
});

// 注册 JWT 相关服务
builder.Services.AddJwtTokenService(builder.Configuration);
builder.Services.AddJwtAuthentication(builder.Configuration);

builder.Services.AddInfrastructure(builder.Configuration);

builder.Services.AddApplication();

var app = builder.Build();

// 启用CORS - 必须在UseRouting之前
app.UseCors("AllowAll");

// 添加静态文件中间件
app.UseStaticFiles();

// 添加路由中间件
app.UseRouting();

// 添加授权中间件（如果需要）
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();